#include "BBank.h"

BBank::BBank(glm::vec3 _Pos, float _r, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder) :
		Building(_Pos, _r, _Name, _modelFName, _mPool, _pathfinder)
{
	type = OBJ_BBANK;
	parentType = OBJ_BUILDING;

	TaxTargets = std::vector<Building *>();
}

BBank::~BBank(void)
{
}

void BBank::GetTaxTargets()
{
	std::vector<void*> ent = pathfinder->getEntityOfTypeInArea(gPoint,
			activityRadius, OBJ_BUILDING, STATUS_READY);

	for (unsigned int i = 0; i < ent.size(); i++)
		if (ent[i] != NULL)
		{
			Building *building = static_cast<Building*>(ent[i]);

			if (building->getType() != OBJ_BBANK)
				TaxTargets.push_back(building);
		}
}

Building * BBank::getOneTarget()
{
	Building* buf = NULL;

	if (TaxTargets.size() > 0)
	{
		buf = TaxTargets[TaxTargets.size() - 1];
		TaxTargets.pop_back();
	}

	return buf;
}

void BBank::control(float t)
{
	if ((underConstruction) && timer.GetInterval(10))
		updateAge();

	if (slaves.size() > 0)
	{
		if ((chckAreaTimer.GetInterval(60000)) && (TaxTargets.size() == 0))
		{
			GetTaxTargets();
			gCon::con->print("%s: Tax area updated, %i objects found.", Name.c_str(),
					TaxTargets.size());
		}

		if ((getTaxTimer.GetInterval(5000)) && (TaxTargets.size() > 0))
		{
			for (unsigned int i = 0; i < slaves.size(); i++)
			{
				if (slaves[i] != NULL)
				{
					if ((slaves[i]->getStatus() == STATUS_READY)
							|| (slaves[i]->getStatus() == STATUS_GOHOME))
					{
						Taxman *taxman = static_cast<Taxman*>(slaves[i]);
						taxman->setWork(getOneTarget());
					}
				}
			}
		}
	}

}